package com.fzy.core.system.action;

import ge.core.system.entity.Entity;
import ge.core.system.entity.Mapping;
import ge.core.system.entity.Module;
import ge.core.system.entity.Property;

import java.util.Date;

import com.fzy.core.system.service.DbService;
import com.fzy.core.utils.U;
import com.fzy.core.webwork.BaseAction;
import com.fzy.core.webwork.MyValidationAware;
import com.opensymphony.xwork.ModelDriven;
import com.opensymphony.xwork.Preparable;

public class PropertyInputAction extends BaseAction implements
		MyValidationAware, Preparable, ModelDriven {

	private static final long serialVersionUID = -6545776513894162109L;
	String errorDispatch;
	Property property;
	DbService dbService;
	boolean isEntityFound;

	public void prepare() throws Exception {
		errorDispatch = "dispatcher:/pages/refresh-parent.jsp";
		property = (Property) validateIdAndGetEntity(request.getParameter("id"),
				Property.class);

		// only for delete operation
		isEntityFound = property != null;

		// default values
		if (property == null) {
        	property = new Property();
        	property.setIsKey("0");
        	property.setIsNotNull("0");
        	property.setIsQuery("0");
        	property.setIsShow("1");
        	property.setIsUnique("0");
        	property.setIsWork("1");
        	property.setPropertyType("string");
		}
	}

	public String execute() throws Exception {
		return "dispatcher:/WEB-INF/jsp/core/system/property-input.jsp";
	}

	public String delete() throws Exception {
		if (isEntityFound) {
			property.setStatus("0");
			property.setUpdatedDate(new Date());
			property.setUpdatedUser(U.getUser());
			dbService.update(property);

			addActionMessage(getText("success.save"));
		} else {
			addActionError(getText("error.entityMissing",
					new String[] { request.getParameter("id") }));
		}
		return "dispatcher:/pages/refresh-parent.jsp";
	}

	public String getErrorDispatch() {
		return errorDispatch;
	}

	public void setDbService(DbService dbService) {
		this.dbService = dbService;
	}

	public Object getModel() {
		return property;
	}

}
